﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Transcend.FileUploader.Entity
{
	/// <summary>
	/// dsinstance描述类
	/// </summary>
	public class DocInstance
	{

		/// <summary>
		/// 宗图像ID
		/// </summary>
		public decimal Id { get; set; }


		public int IsDeleted { get; set; }

		/// <summary>
		/// 应用类型ID
		/// </summary>
		public decimal AppTypeId { get; set; }

		/// <summary>
		/// 文档应用主键ID
		/// </summary>
		public string Pk { get; set; }

		/// <summary>
		/// 最后操作时间默认 DateTime.Now
		/// </summary>
		public DateTime LastOpTime { get; set; }

		/// <summary>
		/// 操作人
		/// </summary>
		public string LastOperater { get; set; }

		/// <summary>
		/// 宗号
		/// 注意：该字段在可能当前扫描库中不存在，为如果要使用该字段请在dsinstance加入该字段
		/// </summary>
		public string DocId { get; set; }

		/// <summary>
		/// 自编档案号
		/// 注意：该字段在可能当前扫描库中不存在，为如果要使用该字段请在dsinstance加入该字段
		/// </summary>
		public string DocNo { get; set; }

		/// <summary>
		/// FTP存储、访问路径
		/// </summary>
		public string Uri { get; set; }

        public int DIVISION_ID { get; set; }

        /// <summary>
        /// 是否分区
        /// </summary>
        public bool isPartition { get; set; }

		public int Pages
		{
			get
			{
				int pages = 0;

				foreach (DocCategory cate in Category)
				{
					pages += cate.DocPage.Count;
				}
				return pages;
			}
		}

		public decimal Size
		{
			get
			{
				long size = 0;

				foreach (var cate in Category)
				{
					foreach (var p in cate.DocPage)
					{
						size += p.Size;
					}
				}

				return size;
			}
		}

		/// <summary>
		/// 卷号
		/// 注意：该字段在可能当前扫描库中不存在，为如果要使用该字段请在dsinstance加入该字段
		/// </summary>
		public string VolNo { get; set; }

		/// <summary>
		/// 宗名称
		/// </summary>
		public string Text { get; set; }

		/// <summary>
		/// 用于文档上传时，保存本地文件夹路径
		/// </summary>
		public string LocalDir { get; set; }

		/// <summary>
		/// 当前分类下的文档信息
		/// 注意：添加文档请使用Add方法，不要使用IList.Add()方法
		/// </summary>
		public List<DocCategory> Category { get; set; }

		/// <summary>
		/// 通过给定文档ID找到文档信息并返回，为找到返回null
		/// </summary>
		/// <param name="imgId"></param>
		/// <returns></returns>
		public DocPage GetDocPageById(decimal imgId)
		{
			DocPage page = null;
			foreach (DocCategory c in Category)
			{
				foreach (DocPage p in c.DocPage)
				{
					if (p.Id == imgId) return p;
				}
			}
			return page;
		}

		#region 构造函数

		public DocInstance()
			: this(0, 0, string.Empty, string.Empty) { }

		public DocInstance(decimal instid, decimal apptypeid, string pkvalue, string lastoperater)
			: this(instid, apptypeid, pkvalue, DateTime.Now, lastoperater, string.Empty, string.Empty, string.Empty) { }

        public DocInstance(decimal instid, decimal apptypeid, string pkvalue, DateTime lastoptime, string lastoperater, string recordkey, string archivesid, string volno)
		{
			Id = instid;
			AppTypeId = apptypeid;
			Pk = pkvalue;
			LastOpTime = lastoptime;
			LastOperater = lastoperater;
			DocId = recordkey;
			DocNo = archivesid;
			VolNo = volno;
			DocAttr = new Dictionary<string,object>();
			Category = new List<DocCategory>(1);
		}

		#endregion

		/// <summary>
		/// 添加分类项，如果相同名称的分类已存，将使用已有的分类
		/// </summary>
		/// <param name="c"></param>
		/// <returns></returns>
		public DocCategory Add(DocCategory c)
		{
			DocCategory ret = c;

			if (c == null) return c;
			var idx = Category.FindIndex(item => item.Name.Equals(c.Name));
			// 电子文档分类不存在
			if (idx == -1)
			{
				Category.Add(c);
			}
			else
			{
				ret = Category[idx];
				ret.Add(c.DocPage);
			}

			ret.InstId = this.Id;
			ret.Document = this;
			return ret;
		}
		/// <summary>
		/// 添加分类项，如果相同名称的分类已存，将使用已有的分类
		/// </summary>
		/// <param name="itemid"></param>
		/// <param name="rowno"></param>
		/// <param name="credname"></param>
		/// <param name="instid"></param>
		/// <returns></returns>
		public DocCategory Add(decimal itemid, int rowno, string credname)
		{
			DocCategory c = new DocCategory(itemid, rowno, credname, Id);
			return Add(c);
		}
		/// <summary>
		/// 添加分类项，如果相同名称的分类已存，将使用已有的分类
		/// </summary>
		/// <param name="cate"></param>
		public void Add(IList<DocCategory> cate)
		{
			foreach (DocCategory c in cate)
			{
				Add(c);
			}
		}
		public override bool Equals(object obj)
		{
			if (obj == null || !(obj is DocInstance)) return false;
			var doc = (DocInstance)obj;
			var temp1 = Pk + '*' + AppTypeId + '*' + Id + '*' + DocId + '*' + DocNo + '*' + VolNo;
			var temp2 = doc.Pk + '*' + doc.AppTypeId + '*' + doc.Id + '*' + doc.DocId + '*' + doc.DocNo + '*' + doc.VolNo;
			return temp1.Equals(temp2, StringComparison.Ordinal);
		}

		public Dictionary<string, object> DocAttr { get; set; }
	}
}
